/*
包含多个接收组件通知触发mutation调用间接更新状态的方法的对象
*/
import {ADD_TODO, DEL_TODO, SELECT_ALL_TODO, CLEAR_ALL_COMPLETED, RECEIVE_TODOS} from './mutation-types'
import storageUtil from '../util/storageUtil'

export default {
  //增加todo
  addTodo ({commit}, todo) {
    //提交对mutation的请求
    commit(ADD_TODO, {todo})
  },
  //删除todo
  delTodo ({commit}, index) {
    //提交对mutation的请求
    commit(DEL_TODO, {index})
  },
  //全选或全不选
  selectAllTodos ({commit}, isCheck) {
    commit(SELECT_ALL_TODO, {isCheck})
  },
  //删除全部选中的
  clearAllCompleted ({commit}) {
    commit(CLEAR_ALL_COMPLETED)
  },
  //异步获取todos并更新状态
  reqTodos ({commit}) {
    //模拟
    setTimeout(() => {
      //读取数据
      const todos = storageUtil.readTodos()
      //提交mutation
      commit(RECEIVE_TODOS, todos)
    }, 1000)
  }
}
